package com.cockatiel.statemachine.cola.statemachine;

import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.StateMachineFactory;
import com.cockatiel.statemachine.enums.ActivityEvent;
import com.cockatiel.statemachine.enums.ActivityState;
import com.cockatiel.statemachine.enums.ChannelTypeEnum;
import com.cockatiel.statemachine.model.StatemachineContext;

import java.util.HashMap;
import java.util.Map;

/**
 * 状态机引擎工厂类
 *
 * @Author: louxiujun@kanzhun.com
 * @Date: 2023-06-11 08:51:09
 * @Description:
 */
public class StatusMachineEngine {

    private StatusMachineEngine() {
    }

    private static final Map<ChannelTypeEnum, String> STATUS_MACHINE_MAP = new HashMap();

    static {
        // 短信推送状态
        STATUS_MACHINE_MAP.put(ChannelTypeEnum.SMS, "smsStateMachine");
        // 微信图文推送状态
        STATUS_MACHINE_MAP.put(ChannelTypeEnum.WEICHAT, "weichatStateMachine");
        // App PUSH推送状态
        STATUS_MACHINE_MAP.put(ChannelTypeEnum.PUSH, "pushStateMachine");
        //......
    }

    public static String getMachineEngine(ChannelTypeEnum channelTypeEnum) {
        return STATUS_MACHINE_MAP.get(channelTypeEnum);
    }

    /**
     * 触发状态转移
     * @param channelTypeEnum
     * @param status 当前状态
     * @param eventType 触发事件
     * @param context 上下文参数
     */
    public static void fire(ChannelTypeEnum channelTypeEnum, ActivityState status, ActivityEvent eventType, StatemachineContext context) {
        StateMachine stateMachine = StateMachineFactory.get(STATUS_MACHINE_MAP.get(channelTypeEnum));
        //推动状态机进行流转，具体介绍本期先省略
        stateMachine.fireEvent(status, eventType, context);
    }
}
